package com.zyz.M_File和IO流.File类.练习;

import java.io.File;
import java.io.IOException;

/**
 * @author: vick
 * @Date: 2025/4/7
 * @Description:
 * @version: 1.0
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        File file = new File("testDir");
        if (!file.exists()) {
            if (file.mkdirs()) {
                System.out.println("创建成功");
            }else {
                System.out.println("创建失败");
            }
        }

        //在目录中创建多个文件和目录
        createFileAndDir(file);

        deleteFile(file, "aa.txt");
    }

    private static void deleteFile(File file, String fileName) {
        File file1 = new File(file, fileName);
        if (file1.exists() && file1.isFile()) {
            if (file1.delete()) {
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }else {
            System.out.println("文件不存在");
        }
    }

    public static void createFileAndDir(File file) throws IOException {
        File file1 = new File(file, "aa.txt");
        if (file1.createNewFile()) {
            System.out.println("文件创建成功");
        }

        File file2 = new File(file, "bb.txt");
        if (file2.createNewFile()) {
            System.out.println("文件创建成功");
        }

        File file3 = new File(file, "dir1");
        if (file3.mkdirs()) {
            System.out.println("目录创建成功");
        }

        File file4 = new File(file, "dir2");
        if (file4.mkdirs()) {
            System.out.println("目录创建成功");
        }
    }
}
